package com.stox.core.ui.swing.view;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.stox.core.event.ApplicationEventListener;
import com.stox.core.event.ApplicationEventPublisher;
import com.stox.core.event.ExchangeAddedEvent;
import com.stox.core.manager.ExchangeManager;
import com.stox.core.model.Exchange;
import com.stox.core.ui.swing.action.UpdateExchangeSelectionAction;
import com.stox.core.ui.swing.wrapper.JPanel;
import com.stox.core.ui.swing.wrapper.JTable;
import com.stox.ioc.Container;
import com.stox.resources.i18n.MessageSource;
import com.stox.resources.image.ImageIconLoader;

public class ExchangeConfigurationView extends InternalFrameView {
	private static final long serialVersionUID = 6892791656870789057L;
	
	private JTable table = new JTable();
	private ExchangeManager exchangeManager;
	
	public ExchangeConfigurationView() {
		super(MessageSource.getMessage("Exchange.Configuration"), false, true, false, true);
		this.exchangeManager = Container.get(ExchangeManager.class);
				
	}

	@Override
	public void afterPropertiesSet() {
		super.afterPropertiesSet();
		
		JPanel controlPanel = new JPanel();
		
		final List<Exchange> exchanges = exchangeManager.findAll();
		String[] columnNames = 
			{	MessageSource.getMessage("Name"),
				MessageSource.getMessage("Country"),
				MessageSource.getMessage("Flag")};
		
		Object[][] data = new Object[exchanges.size()][columnNames.length];
		for(int i = 0; i < exchanges.size(); i++){
			data[i] = createRowData(exchanges.get(i));
		}
	
		final DefaultTableModel model = new DefaultTableModel(data, columnNames);
		table.setModel(model);
		ApplicationEventPublisher.subscribe(new ApplicationEventListener(){
			@Override
			public boolean supports(Class<?> clazz) {
				return clazz.isAssignableFrom(ExchangeAddedEvent.class);
			}
			@Override
			public void onApplicationEvent(Object object) {
				ExchangeAddedEvent event = (ExchangeAddedEvent)object;
				model.addRow(createRowData((Exchange)event.getSource()));
				table.revalidate();
			}
		});
		
		for(Exchange exchange : exchanges){
			if(exchange.isConfigured()){
				int index = exchanges.indexOf(exchange);
				table.setRowSelectionInterval(index, index);
			}
		}
		
		
		JScrollPane scrollPane = new JScrollPane(table);
		controlPanel.add(scrollPane, BorderLayout.CENTER);
		
		JButton updateButton = new JButton(MessageSource.getMessage("Update"));
		updateButton.addActionListener(new UpdateExchangeSelectionAction(table));
		controlPanel.add(updateButton, BorderLayout.SOUTH);
		getContentPane().add(controlPanel, BorderLayout.CENTER);
	}
	
	private Object[] createRowData(Exchange exchange){
		Object[] objects = new Object[3];
		objects[0] = exchange.getName();
		objects[1] = exchange.getCountry();
		objects[2] = ImageIconLoader.loadImageIcon(exchange.getFlagImage());
		return objects;
	}
}
